﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using StockAnalyzer.Core;
using StockAnalyzer.Store;

namespace StockAnalyzer.Analyzer
{
    class PriceAnalyzer
    {
        private readonly StockCollection stocks;

        public PriceAnalyzer(StockCollection stocks)
        {
            this.stocks = stocks;
        }
        
        public void Analyse()
        {
            var list = new List<StockWithPricePosition>();
            foreach (Stock s in stocks)
            {
                if (s.History.Count > 0)
                {
                    var pps = new StockWithPricePosition(s);
                    var high = s.History.Max<Tick>();
                    var low = s.History.Min<Tick>();
                    var current = s.History[0];
                    if (high.Close - low.Close != 0)
                    {
                        pps.PricePosition = (current.Close - low.Close) / (high.Close - low.Close);
                    }

                    //成交量太小的股票没什么意义
                    if (s.History[0].Volume >= Utility.VolumeFilter)
                    {
                        list.Add(pps);
                    }
                }
            }
            list.Sort(new StockPositionComparer());
            save(list);
        }

        private static void save(List<StockWithPricePosition> list)
        {
            var fs = new FileStream(Utility.TargetPath + "\\stock_priceposition.txt", FileMode.Create);
            var w = new StreamWriter(fs);
            try
            {
                foreach (StockWithPricePosition s in list)
                {
                    string line = s.stock.Symbol.ToUpper()
                                  + "  :  percent="
                                  + s.PricePosition.ToString("P02")
                                  +", current="
                                  + s.stock.History[0].Close
                                  + ", high="
                                  + s.stock.History.Max<Tick>().Close
                                  + ", Low="
                                  + s.stock.History.Min<Tick>().Close;
                    w.WriteLine(line);
                }
                w.Flush();
            }
            finally
            {
                fs.Close();
            }
        }
    }

    internal class StockWithPricePosition:StockExtend
    {
        public double PricePosition;
        public StockWithPricePosition(Stock stock)
            : base(stock)
        {
        }

        public override string ToString()
        {
            return stock.Symbol + "  :  " + PricePosition.ToString("P02");
        }
    }

    class StockPositionComparer : IComparer<StockWithPricePosition>
    {
        #region IComparer<Stock> Members

        public int Compare(StockWithPricePosition x, StockWithPricePosition y)
        {
            if (y.PricePosition > x.PricePosition)
                return -1;
            else if (y.PricePosition == x.PricePosition)
                return 0;
            else
                return 1;
        }

        #endregion
    }
}